package com.wbs.service.impl;

import com.alibaba.nacos.shaded.org.checkerframework.checker.units.qual.A;
import com.wbs.mapper.TbSpecificationOptionMapper;
import com.wbs.model.TbSpecification;
import com.wbs.model.TbSpecificationOption;
import com.wbs.model.TbSpecificationOptionExample;
import com.wbs.service.SpecificationOptionService;
import com.wbs.vo.SpecificationOptionVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: 芝梦
 * @Description:
 * @DateTime: 2024/1/24 18:26
 */
@Service
public class SpecificationOptionServiceImpl implements SpecificationOptionService {
    @Autowired
    private TbSpecificationOptionMapper specificationOptionMapper;

    @Override
    public List<SpecificationOptionVo> selectByExample(Long specId) {
        TbSpecificationOptionExample specificationOptionExample = new TbSpecificationOptionExample();
        specificationOptionExample.createCriteria().andSpecIdEqualTo(specId);

        List<TbSpecificationOption> tbSpecificationOptions = specificationOptionMapper.selectByExample(specificationOptionExample);
        List<SpecificationOptionVo> collect = tbSpecificationOptions.stream().map((x) -> {
            SpecificationOptionVo specificationOptionVo = new SpecificationOptionVo();
            BeanUtils.copyProperties(x, specificationOptionVo);
            return specificationOptionVo;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public void deleteBySpecId(Long id) {
        TbSpecificationOptionExample specificationOptionExample = new TbSpecificationOptionExample();
        specificationOptionExample.createCriteria().andSpecIdEqualTo(id);
        specificationOptionMapper.deleteByExample(specificationOptionExample);
    }

    @Override
    public void addList(List<SpecificationOptionVo> list) {
        list.forEach(x -> {
            TbSpecificationOption tbSpecificationOption = new TbSpecificationOption();
            BeanUtils.copyProperties(x, tbSpecificationOption);
            specificationOptionMapper.insertSelective(tbSpecificationOption);
        });
    }

    @Override
    public List<SpecificationOptionVo> findAllOption() {
        List<TbSpecificationOption> tbSpecificationOptionList = specificationOptionMapper.selectByExample(null);
        //转换
        List<SpecificationOptionVo> optionVoList = tbSpecificationOptionList.stream().map(this::conversionVo).collect(Collectors.toList());
        return optionVoList;
    }

    /*辅助方法---------------------------------------------------------------------------------------------------------------------------------------------*/
    //TbSpecificationOption 转换为 SpecificationOptionVo
    private SpecificationOptionVo conversionVo(TbSpecificationOption tbSpecificationOption){
        SpecificationOptionVo specificationOptionVo = new SpecificationOptionVo();
        BeanUtils.copyProperties(tbSpecificationOption,specificationOptionVo);
        return specificationOptionVo;
    }

}
